package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.WorkSubmit;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 作业提交dao
 * @author Administrator
 *
 */
public class WorkSubmitDaoImp implements CommnonDao<WorkSubmit>{

	@Override
	public long save(WorkSubmit value) throws Exception {
		String sql="insert into t_work_submit values(null,?,?,?,?,?,?);";
		long num=JdbcUtil.insertAuto(sql, value.getHomewordId(),
				value.getStuId(),
				value.getWorkContent(),
				value.getWorkFile(),
				value.getCreateTime(),
				value.getWorkState());
		return num;
	}

	@Override
	public int update(WorkSubmit value) throws Exception {
		return 0;
	}

	@Override
	public int delete(WorkSubmit value) throws Exception {
		return 0;
	}

	@Override
	public List<WorkSubmit> findAll(int pageNo, int pageSize) throws Exception {
		return null;
	}

	//根据老师编号查询作业
	@SuppressWarnings("unchecked")
	@Override
	public List<WorkSubmit> findAll(Object key, int pageNo, int pageSize)
			throws Exception {
		String sql="SELECT h.class_name,stu.stu_name,s.create_time,s.work_file,s.work_content,q.work_level,s.work_submit_id"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id"
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homework_id=s.homeword_id"
				+ " WHERE h.tch_id=?"
				+ " limit ?,?;";
		List<WorkSubmit> works=JdbcUtil.query(WorkSubmit.class, sql, key,(pageNo-1)*pageSize,pageSize);
		return works;
	}
	
	@SuppressWarnings("unchecked")
	public List<WorkSubmit> findAll(int tchId, String content,String className, Integer pageNo,
			int pageSize) throws Exception {
		String sql="SELECT h.class_name,stu.stu_name,s.create_time,s.work_content,s.work_file,q.work_level,q.work_quality_id"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id "
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homework_id=s.homeword_id"
				+ " WHERE h.tch_id=? AND stu.stu_name=? AND stu.stu_class_name=?"
				+ " limit ?,?;";
		List<WorkSubmit> works=JdbcUtil.query(WorkSubmit.class, sql, tchId,content,className,(pageNo-1)*pageSize,pageSize);
		return works;
	}

	//根据作业编号查询提交作业
	@SuppressWarnings("unchecked")
	public List<WorkSubmit> queryByhwId(Object key, int pageNo, int pageSize)
			throws Exception {
		String sql="SELECT h.class_name,stu.stu_name,s.create_time,s.work_file,s.work_content,q.work_level,s.work_submit_id"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id"
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homeword_id=s.homeword_id"
				+ " WHERE h.homeword_id=?"
				+ " limit ?,?;";
		List<WorkSubmit> works=JdbcUtil.query(WorkSubmit.class, sql, key,(pageNo-1)*pageSize,pageSize);
		return works;
		
	}
		
	@Override
	public WorkSubmit findByKey(Object key) throws Exception {
		return null;
	}

	@Override
	public List<Map<String, Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 批改作业页面页数查询
	 * @param pageSize
	 * @param tchId
	 * @return
	 * @throws Exception
	 */
	public long selectPages(int pageSize, int tchId) throws Exception {
		String sql="SELECT count(*)"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id"
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homework_id=s.homeword_id"
				+ " WHERE h.tch_id=?";
		long page=(long) JdbcUtil.uniqueValue(sql, 1, tchId);
		return page%pageSize==0?page/pageSize:page/pageSize+1;
	}

	public long selectPages(int pageSize, int tchId, String content,String className) throws Exception {
		String sql="SELECT count(*)"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id "
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homeword_id=s.homeword_id"
				+ " WHERE h.tch_id=? AND stu.stu_name=? AND stu.stu_class_name=?";
		long pages=(long) JdbcUtil.uniqueValue(sql, 1, tchId,content);
		return pages%pageSize==0?pages/pageSize:pages/pageSize+1;
	}

	@SuppressWarnings("unchecked")
	public List<WorkSubmit> findAll(int tchId, String className, Integer pageNo, int pageSize) throws Exception {
		String sql="SELECT h.class_name,stu.stu_name,s.create_time,s.work_content,s.work_file,q.work_level,q.work_quality_id"
				+ " FROM t_work_submit AS s"
				+ " LEFT JOIN t_work_quality AS q ON q.work_submit_id=s.work_submit_id "
				+ " LEFT JOIN t_student AS stu ON stu.stu_id=s.stu_id"
				+ " LEFT JOIN t_homework AS h ON h.homework_id=s.homeword_id"
				+ " WHERE h.tch_id=? AND stu.stu_class_name=?"
				+ " limit ?,?;";
		List<WorkSubmit> works=JdbcUtil.query(WorkSubmit.class, sql, tchId,className,(pageNo-1)*pageSize,pageSize);
		return works;
	}

	/**
	 * 查询学生是否已提交作业
	 * @param homeworkId
	 * @param stuId
	 * @return
	 * @throws Exception
	 */
	public Integer checkWork(long homeworkId,long stuId)throws Exception {
		String sql = "select * from t_work_submit where homeword_id = ? and stu_id = ?";
		WorkSubmit ws = (WorkSubmit) JdbcUtil.uniqueBean(WorkSubmit.class, sql, homeworkId, stuId);
		return ws==null?1:2;
	}

}
